package com.wh.weibo.controller;

import com.wh.weibo.common.Constants;
import com.wh.weibo.model.ServiceResult;
import com.wh.weibo.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * @author WeiHong
 * @date 2019/7/8 15:51
 */
@Controller
@RequestMapping("user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private HttpServletRequest httpServletRequest;


    /**
     * 跳转到用户登陆页面
     *
     * @return
     */
    @GetMapping("login.html")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    /**
     * 跳转到用户注册页面
     *
     * @return
     */
    @GetMapping("register.html")
    public ModelAndView register() {
        ModelAndView modelAndView = new ModelAndView("register");
        return modelAndView;
    }

    @PostMapping(value = "login")
    @ResponseBody
    public Map login(@RequestParam(name = "loginName") String loginName,
                     @RequestParam(name = "password") String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ServiceResult result = userService.login(loginName, password);
        if (!result.isSuccess()) {
            return getErrorResp((String) result.getData());
        } else {
            //序列化登录用户的user对象到http session中，并以cookie的方式回传给前端用户登录的信息
            httpServletRequest.getSession().setAttribute("loginUser", result.getData());
            return getResp(result.getData());
        }

    }

    @PostMapping(value = "register")
    @ResponseBody
    public Map register(@RequestParam(name = "loginName") String loginName,
                        @RequestParam(name = "password") String password,
                        @RequestParam(name = "nickName") String nickName,
                        @RequestParam(name = "gender") Integer gender,
                        @RequestParam(name = "age") Integer age,
                        @RequestParam(name = "avatarUrl") String avatarUrl) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        if (StringUtils.isAnyBlank(loginName, password, nickName, avatarUrl)) {
            return getErrorResp("输入参数不合法");
        }
        if (gender != Constants.GENDER.MAN.getCode() && gender != Constants.GENDER.FEMALE.getCode()) {
            return getErrorResp("输入性别不合法");
        }
        ServiceResult result = userService.register(loginName, password, nickName, gender, age, avatarUrl);
        if (result.isSuccess()) {
            return getResp(null);
        } else {
            return getErrorResp(result.getErrorMsg());
        }
    }

}
